package com.xieyong.leetcode;

public class Leetcode2566 {
    public static void main(String[] args) {
        Leetcode2566 leetcode2566 = new Leetcode2566();
        int num = 11891;
        int i = leetcode2566.minMaxDifference(num);
        System.out.println(i);
    }


    public int minMaxDifference(int num) {
        char[] chars = String.valueOf(num).toCharArray();
        char flag = 'a';
        for (int i = 0; i < chars.length; i++) {
            if (flag == 'a') {
                if (chars[i] != '9') {
                    flag = chars[i];
                    chars[i] = '9';
                }
            } else {
                if (chars[i] == flag) {
                    chars[i] = '9';
                }
            }
        }
        int max = Integer.parseInt(String.valueOf(chars));
        char[] chars2 = String.valueOf(num).toCharArray();
        char flag2 = 'b';
        for (int i = 0; i < chars2.length; i++) {
            if (flag2 == 'b') {
                if (chars2[i] != '0') {
                    flag2 = chars2[i];
                    chars2[i] = '0';
                }
            } else {
                if (chars2[i] == flag2) {
                    chars2[i] = '0';
                }
            }
        }
        int min = Integer.parseInt(String.valueOf(chars2));
        return max - min;
    }
}
